package javaApi.activity6;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

public class HDFSFileSystem {
    public void putFileToHDFS() throws IOException, InterruptedException, URISyntaxException{

        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://niit-master:9000"), conf , "niit");

        FileInputStream fis = new FileInputStream(new File("input/input.txt"));

        FSDataOutputStream fos = fs.create(new Path("./input.txt"));

        IOUtils.copyBytes(fis, fos, conf);

        IOUtils.closeStream(fos);
        IOUtils.closeStream(fis);
        fs.close();
    }

    public void getFileFromHDFS() throws IOException, InterruptedException, URISyntaxException{


        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://niit-master:9000"), conf , "niit");


        FSDataInputStream fis = fs.open(new Path("./input.txt"));

        FileOutputStream fos = new FileOutputStream(new File("output/InputDownload.txt"));

        IOUtils.copyBytes(fis, fos, conf);

        IOUtils.closeStream(fos);
        IOUtils.closeStream(fis);
        fs.close();
    }

    public static void main(String[] args) throws InterruptedException, IOException, URISyntaxException {
        HDFSFileSystem hs=new HDFSFileSystem();
        hs.putFileToHDFS();
        hs.getFileFromHDFS();
    }

}

